package com.gserver.game.character;

import java.util.List;

import org.jboss.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gserver.commons.ByteArray;
import com.gserver.netty.domain.Message;

public class Player {

	private static final Logger logger = LoggerFactory.getLogger(Player.class);

	/* 玩家的基本信息 */

	/** 玩家的状态 */
	private volatile int state;

	/** 玩家的当前角色对象 */
	private PlayerCharacter character;

	/** 玩家的所有角色* */
	private List<PlayerCharacter> characters;

	/* 玩家在游戏过程中的数据信息 */

	/** 最后一次发送聊天消息的时间 */
	private transient long lastChatTime;

	/** 玩家的passport * */
	private int passportId;

	/** 玩家的passprot名称 */
	private String passportName;

	private volatile Channel channel;

	/**
	 * 将消息发送给Player
	 * 
	 * @param type
	 *            消息类型号
	 * @param params
	 *            内容
	 * @throws IllegalArgumentException
	 *             msg为空的时候会抛出此异常
	 * @throws IllegalStateException
	 *             与Player绑定的IoSession为空或者没有连接的情况下,会抛出此异常
	 */
	public void sendMessage(short type, short cmd, Object... params) {
		// 仍然是把消息封装在MessagePlayer对象中，再把消息发送出去
		ByteArray byteArray = new ByteArray();
		byteArray.writeArray(params);
		sendMessage(new Message(type, cmd, byteArray.toArray()));
	}

	public void sendMessage(Message msg) {
		if (msg == null) {
			return;
		}

		if (channel == null || !channel.isConnected()) {
			return;
		}
		channel.write(msg);

		logger.debug(
				"[#GS.Player.sendMessage] [The channel( {} ) send msg( handler: {}  cmd: {} ) ]",
				channel.getRemoteAddress(), msg.getHandler(), msg.getCmd());
	}

	public int getPassportId() {
		return passportId;
	}

	public void setPassportId(int passportId) {
		this.passportId = passportId;
	}

	public String getPassportName() {
		return passportName;
	}

	public void setPassportName(String passportName) {
		this.passportName = passportName;
	}

	public Channel getChannel() {
		return channel;
	}

	public void setChannel(Channel channel) {
		this.channel = channel;
	}

}
